﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.ObjectModel;

namespace SharpShare.Configuration {
    public sealed class StorageProvidersSectionGroup : ConfigurationSectionGroup, IKeyedElementCollection<StorageProviderElement> {
        public new ReadOnlyCollection<IExtensionConfigurationSection<StorageProviderElement>> Sections {
            get {
                return ((ConfigurationSectionGroup)this).Sections.Cast<IExtensionConfigurationSection<StorageProviderElement>>().ToList().AsReadOnly();
            }
        }

        #region IKeyedElementCollection<StorageProviderElement> Members

        public StorageProviderElement this[string name] {
            get {
                foreach (IExtensionConfigurationSection<StorageProviderElement> section in this.Sections) {
                    StorageProviderElement element = section[name];
                    if (element != null) {
                        return element;
                    }
                }

                return null;
            }
        }

        public int Count {
            get { return this.Sections.Count; }
        }

        #endregion

        #region IEnumerable<StorageProviderElement> Members

        public IEnumerator<StorageProviderElement> GetEnumerator() {
            return this.Sections.SelectMany(s => s).Cast<StorageProviderElement>().GetEnumerator();
        }

        #endregion

        #region IKeyedElementCollection Members

        ConfigurationElement IKeyedElementCollection.this[string name] {
            get {
                return this[name];
            }
        }

        void IKeyedElementCollection.Add(ConfigurationElement element) {

        }
        void IKeyedElementCollection.Remove(ConfigurationElement element) {

        }
        ConfigurationElement IKeyedElementCollection.CreateElement() {
            return null;
        }
        StorageProviderElement IKeyedElementCollection<StorageProviderElement>.CreateElement() {
            return null;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        #endregion
    }
}
